package com.spzx.file.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Minio 配置信息
 *
 * @author spzx
 */
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig
{
    /**
     * 服务地址
     */
    private String url;

    /**
     * 用户名
     */
    private String accessKey;

    /**
     * 密码
     */
    private String secretKey;

    /**
     * 存储桶名称
     */
    private String bucketName;

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public String getAccessKey()
    {
        return accessKey;
    }

    public void setAccessKey(String accessKey)
    {
        this.accessKey = accessKey;
    }

    public String getSecretKey()
    {
        return secretKey;
    }

    public void setSecretKey(String secretKey)
    {
        this.secretKey = secretKey;
    }

    public String getBucketName()
    {
        return bucketName;
    }

    public void setBucketName(String bucketName)
    {
        this.bucketName = bucketName;
    }

    @Bean
    public MinioClient getMinioClient()
    {
        MinioClient minioClient = MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();

        String bucketName = "spzx"; // 桶名称
        String region = "us-east-1"; // 区域，根据你的设置选择合适的区域
        String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::" + bucketName + "/*\"}]}"; // 策略内容，这里仅允许GET操作，你可以根据需要调整策略内容。
        try{
            // 确保桶存在，如果不存在则创建它。
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).region(region).build());
            }

            // 设置桶策略
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
        } catch (Exception e) {
            System.err.println("Error occurred: " + e);
        }
        return minioClient;
//     ⠰⢷⢿⠄
//⠀⠀⠀⠀⠀⣼⣷⣄
//⠀⠀⣤⣿⣇⣿⣿⣧⣿⡄
//⢴⠾⠋⠀⠀⠻⣿⣷⣿⣿⡀
//🏀⠀⢀⣿⣿⡿⢿⠈⣿
//⠀⠀⠀⢠⣿⡿⠁⠀⡊⠀⠙
//⠀⠀⠀⢿⣿⠀⠀⠹⣿
//⠀⠀⠀⠀⠹⣷⡀⠀⣿⡄
//⠀⠀⠀⠀⣀⣼⣿⠀⢈⣧


    }
}
